import { defineStore } from "pinia";
import {
    getCardList,
    getuserInfo,
    getShareList,
    getHistory, 
    getChatInfo, 
    getMycollectionListByType,
    getApprovalList,
    getApprovalDetail, 
    queryagreementList, 
    getCollectionList
     } from '@/api/index';
import { userInfo, messageInfo, card } from "@/model";
import { PersistedStateOptions } from "pinia-plugin-persistedstate";
interface state {
    cardList: Array<card>,
    detailList: card,
    userAttentList: Array<string>,
    chaterInfoList: Array<userInfo>,
    currentInfo: userInfo,
    userInfo: userInfo,
    messageList: Array<messageInfo>,
    myApprovalList: Array<any>,
    myApplicationList: Array<any>,
    approvalDetail: any,
    applicationDetail: any,
    agreementList: any[],
    agreementDetail: any,
    showOverlay: boolean,
    postsType: string,
    shareList: any[],
    collectList: any[],
    realShow: boolean,
    mycollectionList: any[],
    myhistoryList:any[]
}
export const userStore = defineStore('user', {
    state: (): state => {
        return {
            cardList: [],
            detailList: {} as card,
            chaterInfoList: [],
            currentInfo: {} as userInfo,
            userInfo: {} as userInfo,
            messageList: [],
            userAttentList: [],
            myApprovalList: [],
            myApplicationList: [],
            agreementList: [],
            agreementDetail: {},
            approvalDetail: {},
            applicationDetail: {},
            showOverlay: false,
            postsType: '',
            shareList: [],
            collectList: [],
            realShow: false,
            mycollectionList: [],
            myhistoryList:[]
        }
    },
    getters: {
        unReadNum(): number {
            let num: number = 0
            this.messageList.forEach(item => {
                num += item.unReadNum
            })
            return num
        },
        personalCardList(): Array<card> {
            return this.cardList.filter((item) => {
                return item.publisher_id == this.userInfo.user_id
            })
        },
        personalShareList(): Array<any> {
            return this.shareList.filter((item) => {
                return item.sharer_id == this.userInfo.user_id
            })
        },
        userCardList(): Array<card> {
            return this.cardList.filter((item) => {
                return item.publisher_id == this.userInfo.user_id
            })
        },

    },
    actions: {
        findChater(id: string) {
            return this.messageList.find(item => id == item.id)!
        },
        queryMycollectionListByType(type:string){
            getMycollectionListByType({resource_type:type,resource_id:this.userInfo.user_id}).then((res:any)=>{
                this.mycollectionList=res.data||[]
            })
        },
        queryMyhistoryListByType(type:string){
            getHistory({browser_type:type,viewer_id:this.userInfo.user_id}).then((res:any)=>{
                this.myhistoryList=res.data||[]
            })
        },
        
        getCardListInfo() {
            getCardList().then((res: any) => {
                this.setCardList(res.data)
            })
        },
        getShareListInfo() {
            getShareList().then((res: any) => {
                this.setShareList(res.data)
            })
        },
        getDetail(id: string, type: string) {
            let data = type == 'pet' ? this.cardList : this.shareList
            const result = data.find((item: any) => {
                let objId = type == 'pet' ? item.card_id : item.shareposts_id;
                return id == objId
            })
            console.log(result);

            //@ts-ignore
            this.detailList = result || {}
        },
        async getChaterInfo(id: string) {
            await getChatInfo(id).then((res: any) => {
                this.chaterInfoList.push(res.data);
                this.currentInfo = res.data
            })
        },
        setCurrentInfo(id: string) {
            this.currentInfo = this.chaterInfoList.find((item: userInfo) => {
                return id == item.user_id;
            }) || {} as any
        },
        addUnReadNum(id: string) {
            this.findChater(id).unReadNum++
        },
        addMessage(data: { message: string; senderId: string; receiverId: string; type: string }, id: string) {
            this.findChater(id).messageCohort.push(data)
        },
        getNum(id: string): number {
            return this.findChater(id)?.unReadNum || 0
        },
        resetNum(id: string) {
            this.findChater(id).unReadNum = 0
        },
        addMessageInfo(id: string) {
            this.messageList.push({
                id: id,
                messageCohort: [],
                unReadNum: 0
            })
        },
        getMessageList(id: string) {
            let index = this.messageList.findIndex(item => {
                return id == item.id;
            })
            if (index !== -1)
                return this.messageList[index].messageCohort
        },
        getLasterMessage(id: string) {
            let list = this.findChater(id)?.messageCohort
            if (list.length > 0) {
                return list[list.length - 1].message
            }
            else {
                return ''
            }

        },
        getLasterMessageTime(id: string) {
            let list = this.findChater(id).messageCohort
            if (list.length > 0) {
                return list[list.length - 1]?.time || ''
            }
            else {
                return ''
            }

        },
        setRealShow(show: boolean) {
            this.realShow = show
        },
        setCardList(data: any) {
            this.cardList = data
        },
        setShareList(data: any) {
            this.shareList = data
        },
        setShowOverlay(data: any) {
            this.showOverlay = data
        },

        setpostsType(data: any) {
            this.postsType = data
        },

        setChaterInfoList(data: any) {
            this.chaterInfoList = data
        },

        setApprovalDetail(id: string) {
            getApprovalDetail(id).then((res: any) => {
                this.approvalDetail = res.data[0]
            })
        },

        setUserInfo(data: any) {
            this.userInfo = data
        },
        changeUserInfo(data: any) {
            getuserInfo({user_id:this.userInfo.user_id}).then(res=>{
                this.userInfo = res.data
            })
            
        },
        async getMyapprovalList(type: string) {
            await getApprovalList(this.userInfo.user_id, type).then((res: any) => {
                this.myApplicationList = res.data
            })
        },
        async getagreementList() {
            await queryagreementList(this.userInfo.user_id).then((res: any) => {
                this.agreementList = res.data
            })
        },
        clearMyapprovalList() {
            this.myApplicationList = []
            this.agreementList = []
            this.applicationDetail = {}
            this.agreementDetail = {}
        },
        async queryCollectionList(type: string) {
            return await getCollectionList({ user_id: this.userInfo.user_id, resource_type: type }).then((res: any) => {
                this.collectList = res.data
            })
        },
    },
    /* persist: {
        enabled: true,	//开启
        storage: localStorage,	//修改存储位置
        key: 'userInfo',	//设置存储的key
        paths: ['userInfo', 'cardList', 'messageList', 'chaterInfoList'],//指定要长久化的字段
    } as PersistedStateOptions */
})


